package v3.chapter2.StringAndSyn2; 

public class Run {

	public static void main(String[] args) {
		Service service=new Service();
		ThreadA a =new ThreadA(service);
		a.setName("A");
		a.start();
		
		ThreadB b =new ThreadB(service);
		b.setName("B");
		b.start(); 
	}
}


class ThreadA extends Thread {

	private Service service;

	public ThreadA(Service service) {
		super();
		this.service =service;
	}

	@Override
	public void run() { 
		service.print(new Object());
	}

}

class ThreadB extends Thread {

	private Service service;

	public ThreadB(Service service) {
		super();
		this.service =service;
	}

	@Override
	public void run() { 
		service.print(new Object());
	}

}


class Service{
	public static void print(Object stringParam) {
		try {
			synchronized(stringParam) {
				while(true) {
					System.out.println(Thread.currentThread().getName());
					Thread.sleep(1000);
				}
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}